﻿//goog.provide('imc.cmd');
goog.provide('imc.cmd.Auth');

//var idRol = "20120000000000000";
var idRol = "";
var admin;
var repor;

//getUserData --------------------------------------------------------------------------------------------------------------
imc.cmd.Auth.getUserData = function () {
    imc.cmd.Loader.show();
    $.ajax({
        type: "POST",
        url: "Services/Auth.svc/GetUserRolListRol",
        data: '{"idRol":" ' + idRol + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (userData) {
            imc.cmd.Auth.isAuth(userData);
            $('#emailPop').html(userData.GetUserRolListRolResult["mail"]);
        },
        async: false,
        error: function (result) {
            //alert('Error' + result.status + ' ' + result.statusText + ' ');
            window.location.href = "paginaDeError.html";
        }
    });
}



//isAuth -------------------------------------------------------------------------------------------------------------------
imc.cmd.Auth.isAuth = function (userData) {
    if (userData) {
        idRol = userData.GetUserRolListRolResult["idRol"];
        admin = userData.GetUserRolListRolResult["admin"];
        repor = userData.GetUserRolListRolResult["reporte"]
        if (admin == true) {
            $('#solicitudesMenu').css('display', 'block');
            $('#configuracionMenu').css('display', 'block');
            $('#reportesMenu').css('display', 'block');
            $('#usuarioAdministrador').css('display', 'block');
            $('#consultasMenu').css('display', 'block');
        }
        if (repor == true) {
            $('#solicitudesMenu').css('display', 'block');
            $('#reportesMenu').css('display', 'block');
            $('#usuarioReportes').css('display', 'block');
            $('#consultasMenu').css('display', 'block');
        }
        $('#user').append(userData.GetUserRolListRolResult["user"]);
        $('#rol').append(userData.GetUserRolListRolResult["dscRol"]);
        //$('#userPop').append(userData.GetUserRolListRolResult["user"]);
        $('#nombreUsuario').append(userData.GetUserRolListRolResult["user"]);
        for (var i = 0; i < userData.GetUserRolListRolResult["aIdRol"].length; i++) {
            $('<div/>', {
                'text': userData.GetUserRolListRolResult["aIdRol"][i].rol,
                'id': "itm_" + i,
                'key': userData.GetUserRolListRolResult["aIdRol"][i].idRol,
                'class': "menuPopRol",
                'click': function () {
                    //var x = $(this).val('userData.GetUserRolListRolResult["aIdRol"][i].rol');
                    $("#rol").html($(this).text());
                    idRol = $(this).attr("key");
                    imc.cmd.Auth.pm2.setVisible(false);
                    imc.cmd.ReloadSolicitudes();
                    return false;
                }
            }).appendTo('#listRolPop');
            //            alert(userData.GetUserRolListRolResult["aIdRol"][i].idRol + userData.GetUserRolListRolResult["aIdRol"][i].rol);
        }
    }
    else {
        //alert('No logeo');
        window.location.href = "NoAccess.html"; //"paginaDeError.html";
    }
}
 
//writeCookie --------------------------------------------------------------------------------------------------------------
imc.cmd.Auth.writeCookie = function (userData) {
    document.cookie = "usuario=Rafa; expires=Tue, 12 Jan 2010 12:23:00 GMT";
}

//PopUp Menu ---------------------------------------------------------------------------------------------------------------
imc.cmd.Auth.popupMenu = function () {
    var pm = new goog.ui.PopupMenu();
    pm.setToggleMode(true);
    pm.decorate(document.getElementById('dMenu'));
}

//popupUsuario -------------------------------------------------------------------------------------------------------------
imc.cmd.Auth.popupUsuario = function () {
    imc.cmd.Auth.pm2 = new goog.ui.Popup(document.getElementById('popUsuario'));
    imc.cmd.Auth.pm2.setAutoHide(true);
    var margin = new goog.math.Box(0, 0, 0, 0);
    var btn = document.getElementById('popupUsuario');
    $('#popupUsuario').click(function () {
        imc.cmd.Auth.pm2.setVisible(false);       
        imc.cmd.Auth.pm2.setVisible(true);
    });
    imc.cmd.Auth.pm2.setPosition(new goog.positioning.AnchoredViewportPosition(btn, goog.positioning.Corner.BOTTOM_LEFT));
    imc.cmd.Auth.pm2.setPinnedCorner(goog.positioning.Corner.TOP_LEFT);
    imc.cmd.Auth.pm2.setMargin(margin);
    //    pm2.setToggleMode(true);
    //    pm2.decorate(document.getElementById('popUsuario'));
}